package ucuserslogic

import (
	"context"
	"go.uber.org/zap"
	"navi-mumbai/app/rpc/model/pg/usercenter"
	"navi-mumbai/common/key/userkey"
	"navi-mumbai/common/utils"
	"navi-mumbai/common/xerr"
	"strconv"

	"navi-mumbai/app/rpc/usercenter/internal/svc"
	"navi-mumbai/app/rpc/usercenter/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetAppInitDataLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetAppInitDataLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAppInitDataLogic {
	return &GetAppInitDataLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 获取app初始化数据
func (l *GetAppInitDataLogic) GetAppInitData(in *pb.GetAppInitDataReq) (*pb.GetAppInitDataResp, error) {
	defer utils.RecoverPanic()

	// app config
	var appConfig *usercenter.AppConfig
	var err error
	appConfig, err = l.svcCtx.AppConfigModel.FindOne(l.ctx, in.AppVer)
	if err != nil && err != usercenter.ErrNotFound {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}
	var rsp pb.GetAppInitDataResp
	rsp.AppConfig = &pb.AppConfig{}
	if appConfig != nil {
		rsp.AppConfig.PayState = appConfig.PayState
		rsp.AppConfig.TextState = appConfig.TextState
		rsp.AppConfig.RankState = appConfig.RankState
	}

	// banner config
	rsp.BannerConfig = &pb.BannerConfig{List: make([]*pb.BannerData, 0)}
	var bannerConfig []*usercenter.BannerConfig
	bannerConfig, err = l.svcCtx.BannerConfigModel.Find(l.ctx, userkey.BannerStateOpen, 1, 20)
	if len(bannerConfig) > 0 {
		for i := 0; i < len(bannerConfig); i++ {
			rsp.BannerConfig.List = append(rsp.BannerConfig.List, &pb.BannerData{
				Id:       bannerConfig[i].Id,
				Image:    bannerConfig[i].BannerImage,
				Url:      bannerConfig[i].Url,
				PagePath: bannerConfig[i].PagePath,
				Order:    bannerConfig[i].BannerOrder,
			})
		}
	}

	// apk config
	var apkConfigs []*usercenter.ApkConfig
	apkConfigs, err = l.svcCtx.UserApkConfigModel.Find(l.ctx, 0, strconv.FormatInt(in.AppVer, 10), nil, nil, 0, 0)
	if err != nil && err != usercenter.ErrNotFound {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}
	rsp.ApkUpdateConfig = &pb.ApkConfig{}
	l.Logger.Infof("apkConfigs: %+v", apkConfigs)
	if len(apkConfigs) > 0 {
		// 获取
		apkConfig := apkConfigs[0]
		rsp.ApkUpdateConfig.Enable = apkConfig.Enable
		rsp.ApkUpdateConfig.Currency = apkConfig.Currency
		rsp.ApkUpdateConfig.Version = apkConfig.Version
		rsp.ApkUpdateConfig.Link = apkConfig.Link
		rsp.ApkUpdateConfig.RewardCoins = apkConfig.RewardCoins
		rsp.ApkUpdateConfig.Picture = apkConfig.Picture
	} else {
		// 如果查到，则给默认值
		rsp.ApkUpdateConfig.Enable = 4
	}

	// apk config
	var serviceUpdateConfigs []*usercenter.ServiceUpdateConfig
	serviceUpdateConfigs, err = l.svcCtx.UserServiceUpdateConfigModel.Find(l.ctx, strconv.FormatInt(in.AppVer, 10), nil, nil, 0, 0)
	if err != nil && err != usercenter.ErrNotFound {
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}
	rsp.ServiceUpdateConfig = &pb.ServiceUpdateConfig{}
	l.Logger.Infof("ServiceUpdateConfig: %+v", serviceUpdateConfigs)
	if len(serviceUpdateConfigs) > 0 {
		// 获取
		serviceUpdateConfig := serviceUpdateConfigs[0]
		rsp.ServiceUpdateConfig.Enable = serviceUpdateConfig.Enable
	} else {
		// 如果没查到，则给默认值
		rsp.ServiceUpdateConfig.Enable = 4
	}
	return &rsp, nil
}
